package fr.km.client.presenter.impl;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;

import fr.km.client.model.Article;
import fr.km.client.model.ModelManager;
import fr.km.client.presenter.api.IArticleEditorPresenter;
import fr.km.client.view.api.IArticleEditorView;

public class ArticleEditorPresenter implements IArticleEditorPresenter {
	private IArticleEditorView view;
	private Article article;
	private ModelManager model;
	
	public ArticleEditorPresenter(Article article, ModelManager model) {
		this.article = article;
		this.model = model;
	}
	
	public void setView(IArticleEditorView view) {
		this.view = view;
		view.getCancelButton().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				cancel();
			}
		});
		
		view.getSaveButton().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				save();
			}
		});
	}
	
	@Override
	public void cancel() {
		view.close();
	}

	@Override
	public void save() {
		article.setContent(view.getKnowledge());
		article.setTags(view.getTags());
		article.setTitle(view.getTitle());
		view.close();
		model.saveArticle(article);
	}

	@Override
	public void loadArticle() {
		view.displayKnowledge(article.getContent());
		view.displayTags(article.getTags());
		view.displayTitle(article.getTitle());
	}

	@Override
	public void displayArticle() {
		loadArticle();
		view.display();		
	}

}
